package top.songxs.gmall.item.biz.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import top.songxs.gmall.common.anno.GmallCache;
import top.songxs.gmall.common.constant.GmallCacheConstant;
import top.songxs.gmall.common.result.Result;
import top.songxs.gmall.feign.product.*;
import top.songxs.gmall.feign.search.GoodsFeignClient;
import top.songxs.gmall.item.biz.SkuDetailRpcService;
import top.songxs.gmall.product.vo.SkuDetailVo;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
public class SkuDetailRpcServiceImpl implements SkuDetailRpcService {
    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private SkuInfoFeignClient skuInfoFeignClient;
    @Autowired
    private SpuSaleAttrAndValueClient spuSaleAttrAndValueClient;

    @Autowired
    private SkuSaleValuesConcatFeignClient skuSaleValuesConcatFeignClient;

    @Autowired
    private SkuDetailVoBySkuIdFeignClient skuDetailVoBySkuIdFeignClient;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @Override
    @GmallCache(cacheKey = GmallCacheConstant.REDIS_SKU_INFO + "#{#params[0]}",
            bloomFilterName = GmallCacheConstant.REDIS_SKU_INFO_BLOOM_FILTER,
            bloomFilterValue = "#{#params[0]}",
            lockName = GmallCacheConstant.REDIS_SKU_INFO_LOCK + "#{#params[0]}"
    )
    public SkuDetailVo getSkuDetailList(Long skuId) {
        return this.getSkuDetailVo(skuId);
    }

    @Override
    public List<Long> getAllSkuId() {
        Result<List<Long>> ids = skuInfoFeignClient.getAllSkuId();
        return ids.getData();
    }

    @Autowired
    private GoodsFeignClient goodsFeignClient;

    @Override
    public void updateHotScore(Long goodsId) {
        Long increment = redisTemplate.opsForValue().increment("sku:hotscore:" + goodsId);
        if (increment % 100 == 0) {
            // 通过fegin远程调用service-search
            goodsFeignClient.updateHotScore(goodsId, increment.intValue());
        }

    }

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    private SkuDetailVo getSkuDetailVo(Long skuId) {
        Result<SkuDetailVo> skuDetailVo = skuDetailVoBySkuIdFeignClient.getSkuDetailVo(skuId);
        SkuDetailVo data = skuDetailVo.getData();
        return data;
       /* SkuDetailVo skuDetailVo = new SkuDetailVo();
        try {
            //通过远程调用skuInfoFeignClient来获取skuInfo的值
            Result<SkuInfo> skuInfo = skuInfoFeignClient.getSkuInfo(skuId);
            skuDetailVo.setSkuInfo(skuInfo.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        //创建countDownLatch
        CountDownLatch countDownLatch = new CountDownLatch(4);
        threadPoolExecutor.execute(() -> {
            //通过远程调用categoryFeignClient来获取所必须要的CategoryView的值
            Result<CategoryView> allCategory = categoryFeignClient.findAllCategory(skuId);
            skuDetailVo.setCategoryView(allCategory.getData());
            countDownLatch.countDown();
        });

        threadPoolExecutor.execute(() -> {
            //为了保证实时性，再调用一次skuInfoFeignClient来获取商品的价格
            Result<SkuInfo> price = skuInfoFeignClient.getPrice(skuId);
            skuDetailVo.setPrice(price.getData().getPrice());
            countDownLatch.countDown();
        });

        threadPoolExecutor.execute(() -> {
            //通过调用skuInfoFeignClient来获取spuSaleAttrList
            Result<List<SpuSaleAttr>> spuSalAttrAndValueBySkuId = spuSaleAttrAndValueClient.findSpuSalAttrAndValueBySkuId(skuId);
            skuDetailVo.setSpuSaleAttrList(spuSalAttrAndValueBySkuId.getData());
            countDownLatch.countDown();
        });

        threadPoolExecutor.execute(() -> {
            //通过调用skuSaleValuesConcatFeignClient来获取拼接销售属性值字符串
            Result<List<AttrValuesVo>> skuSaleValues = skuSaleValuesConcatFeignClient.getSkuSaleValues(skuId);
            List<AttrValuesVo> data = skuSaleValues.getData();
            System.out.println(data);
            Map<String, Long> map = skuSaleValues.getData().stream().collect(Collectors.toMap(
                    AttrValuesVo::getSavIds, AttrValuesVo::getSkuId));
            String valuesSkuJson = JSON.toJSONString(map);
            skuDetailVo.setValuesSkuJson(valuesSkuJson);
            countDownLatch.countDown();
        });
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return skuDetailVo;*/
    }
}
